package com.fun.arithmetic;

import java.util.HashMap;

public class StringMaxSub {
    public static void main(String[] args) {
        String str = "abcadd";
        System.out.println(getMaxLengthNoRepeat(str));
    }

    public static int getMaxLengthNoRepeat(String str) {
        int length = str.length();
        HashMap<Character, Integer> map = new HashMap<>();
        int left = 0;
        int max = 0;
        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            if (map.containsKey(c)) {
                //说明此时有冲突，更新下left的下标
                left = Math.max(left, map.get(c) + 1);
            }
            map.put(c, i);
            max = Math.max(max, i - left + 1);
        }
        return max;
    }
}
